package ar.com.ctrln.view.dashboard.compras.insumo;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.EventQueue;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Spinner;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import ar.com.ctrln.control.mantenimiento.insumo.RegistrarInsumo;
import ar.com.ctrln.persistence.model.insumo.Insumo;
import ar.com.ctrln.persistence.model.proveedor.Proveedor;
import ar.com.ctrln.persistence.model.vehiculo.Vehiculo;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class RegistrarInsumoController extends AbstractMinimizarVentana {

	private static final long serialVersionUID = 7293861791737040132L;

	@WireVariable
	private RegistrarInsumo registrarInsumo;
	
	private Insumo insumo;
	
	private Insumo insumoModif;

	private EventQueue eq = (EventQueue) EventQueues.lookup("refreshInsumos",EventQueues.DESKTOP, true);
	
	private EventQueue act = (EventQueue) EventQueues.lookup("nuevoProveedor",EventQueues.DESKTOP, true);

	@Wire
	private Window wRegistrarInsumo;
	
	@Wire
	private Label lStockInsumo;

	@Wire
	private Textbox tbNuevoInsumo;
	
	@Wire
	private Textbox tbDescripcionInsumo;
				
	@Wire
	private Combobox cboTipoInsumo;
	
	@Wire
	private Combobox cboProoverdorInsumo;
	
	@Wire
	private Spinner spinStockInicial;
	
	@Wire
	private Spinner spinStockRepo;
	
	@Wire
	private Textbox tbUMInsumo;

		
	@SuppressWarnings({"unchecked" })
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		insumoModif = new Insumo();
		if (Executions.getCurrent().getArg().get("data")!=null)
		{
			wRegistrarInsumo.setTitle("Editar Insumo");
			lStockInsumo.setValue("Stock:");
			insumo = (Insumo)Executions.getCurrent().getArg().get("data");
			insumoModif.setId(insumo.getId());
			tbNuevoInsumo.setValue(insumo.getNombre());
			tbDescripcionInsumo.setValue(insumo.getDescripcion());
			spinStockInicial.setValue(insumo.getStock());
			spinStockRepo.setValue(insumo.getNivelReposicion());
			cboTipoInsumo.setText(insumo.getTipoInsumo());
			tbUMInsumo.setValue(insumo.getUnidadMedida());
		}
		//cboProoverdorInsumo.setModel(new ListModelList<Proveedor>(registrarInsumo.listaProveedor()));
		
		act.subscribe(new EventListener<Event>() {
			public void onEvent(Event event) throws Exception {
				cboProoverdorInsumo.setModel(new ListModelList<Proveedor>(registrarInsumo.listaProveedor()));
			}});
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Listen("onCreate = #wRegistrarInsumo")
	public void asignarCombo() {
		cboProoverdorInsumo.addEventListener("onInitRenderLater", new EventListener() {
        	public void onEvent(Event event) throws Exception {
        		if(insumo!=null &&insumo.getProveedor()!=null)
        			cboProoverdorInsumo.setSelectedIndex((int)(insumo.getProveedor().getId()-4));
         	}
   		});
		cboProoverdorInsumo.setModel(new ListModelList<Proveedor>(registrarInsumo.listaProveedor()));    
	  }
			
	@SuppressWarnings("unchecked")
	@Listen("onClick = #btnRegistrarInsumo")
	public void registrarInsumo() {
		insumoModif.setNombre(tbNuevoInsumo.getValue());
		insumoModif.setDescripcion(tbDescripcionInsumo.getValue());
		insumoModif.setStock(spinStockInicial.getValue());
		insumoModif.setNivelReposicion(spinStockRepo.getValue());
		insumoModif.setTipoInsumo(cboTipoInsumo.getValue());
		insumoModif.setUnidadMedida(tbUMInsumo.getValue());
		insumoModif.setProveedor((Proveedor) cboProoverdorInsumo.getModel().getElementAt(cboProoverdorInsumo.getSelectedIndex()));

		String mensaje;
		if (lStockInsumo.getValue()=="Stock:")
		{
			
			registrarInsumo.actualizarInsumo(insumoModif);
			mensaje = "El Insumo se actualizo con exito!";
		}
		else{
			registrarInsumo.guardarInsumo(insumoModif);
			mensaje = "El Insumo se registro con exito!";

		}
		eq.publish(new Event("onButtonClick", null, null));
		wRegistrarInsumo.onClose();
		Clients.showNotification(mensaje,
				Clients.NOTIFICATION_TYPE_INFO, null, "middle_center", 2500);
}
	@Listen("onClick = #iAgregarProveedor")
	public void registrarProveedor() {
		Executions.createComponents(
				"/zul/compras/proveedor/registrarProveedor.zul",
				null, null);
	}
}

